package cn.harry.wx.service.impl;

import cn.harry.wx.dao.WxMsgDao;
import cn.harry.wx.entity.WxMsg;
import cn.harry.wx.service.WxAccountService;
import cn.harry.wx.service.WxMsgService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * 微信消息
 *
 * @author honghh
 * Date 2021-04-08 16:19:27
 * Copyright (C) www.tech-harry.cn
 */
@Service("wxMsgService")
public class WxMsgServiceImpl extends ServiceImpl<WxMsgDao, WxMsg> implements WxMsgService {
    @Resource
    private WxAccountService wxAccountService;

    @Override
    public IPage<WxMsg> getPage(WxMsg wxMsg, Integer pageSize, Integer pageNum) {
        LambdaQueryWrapper<WxMsg> wrapper = new LambdaQueryWrapper<>(wxMsg);
        if (StrUtil.isNotEmpty(wxMsg.getBeginTime())) {
            wrapper.gt(WxMsg::getCreateTime, wxMsg.getBeginTime());
        }
        if (StrUtil.isNotEmpty(wxMsg.getEndTime())) {
            wrapper.lt(WxMsg::getCreateTime, wxMsg.getEndTime());
        }
        return page(new Page<>(pageNum, pageSize), wrapper);
    }


    @Override
    public WxMsg selectById(Long id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public int create(WxMsg wxMsg) {
        return this.baseMapper.insert(wxMsg);
    }


    @Override
    public int update(Long id, WxMsg wxMsg) {
        wxMsg.setId(id);
        return this.baseMapper.updateById(wxMsg);
    }


    @Override
    public int deleteByIds(Long[] ids) {
        return this.baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 记录msg，异步入库
     *
     * @param msg
     */
    @Override
    @Async
    public void addWxMsg(WxMsg msg) {
        this.baseMapper.insert(msg);
    }

}